import logging
import optparse
import chardet

ENCODINGS = {'EUC-TW': 'big5'}

def to_unicode(s, verbose=False):
  if isinstance(s, unicode):
    return s
  if not s:
    return u''
  
  guess = chardet.detect(s)
  logging.debug('Detected encoding %s (conf: %s)', guess['encoding'],
                guess['confidence'])
  enc = ENCODINGS.get(guess['encoding'], guess['encoding'])
  
  try:
    u = unicode(s, enc)
  except UnicodeDecodeError, err:
    u = to_unicode_by_line(s, enc)
  return u


def to_unicode_by_line(s, encoding):
  decoded_lines = []
  
  for line in s.splitlines():
    if not line:
      decoded_lines.append(u'')
      continue
    
    try:
      u = unicode(line, encoding)
    except UnicodeDecodeError, err:
      logging.error('Could not decode %s', line)
      u = line.decode(encoding,'ignore')
    decoded_lines.append(u)
  
  return '\n'.join(decoded_lines)


def main():
  parser = optparse.OptionParser()
  parser.add_option('-d', '--debug', dest='debug', default=False,
                    action='store_true', help='Enable debugging.')
  parser.add_option('-t', '--text', dest='text', default=None,
                    help='Text to encode in UTF-8.')
  
  opts, args = parser.parse_args()
  if len(args) != 0:
    parser.error('Too many arguments')
  
  logging.basicConfig()
  if opts.debug:
    logging.getLogger().setLevel(logging.DEBUG)
  else:
    logging.getLogger().setLevel(logging.INFO)
  
  if not opts.text:
    parser.error('You must specify a text to encode in UTF-8.')
  
  print(to_unicode(opts.text))
  

if __name__ == '__main__':
  main()
